package com.server.http.controller;

import com.server.http.controller.dto.AppInfoDto;
import com.server.libs.ret.RE;
import com.server.libs.ret.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping(value = "/other")
@Slf4j
@Transactional
public class OtherController {
    @Value("${app.version.android}")
    String androidVersion;
    @Value("${app.version.ios}")
    String iosVersion;

    @Value("${app.download.ios}")
    String ios_download;

    @Value("${app.download.android}")
    String android_download;

    @Value("${app.update.content}")
    String content;

    @RequestMapping(value = "/feedback",method = {RequestMethod.GET,RequestMethod.POST})
    public R<String> feedback(@RequestHeader String deviceType, @RequestBody Map<String,Object> data){

        return new R<>();
    }


    @RequestMapping(value = "/upload",method = {RequestMethod.GET,RequestMethod.POST})
    public R<AppInfoDto> upload(@RequestHeader String deviceType, @RequestBody Map<String,Object> data){

        if (deviceType.equals("ios")){
            return getAppInfoDtoR(data, iosVersion, ios_download);

        }else if (deviceType.equals("android")){
            return getAppInfoDtoR(data, androidVersion, android_download);
        }
        throw new RE("设备型号异常："+deviceType);
    }

    private R<AppInfoDto> getAppInfoDtoR(@RequestBody Map<String, Object> data, String version, String url) {
        if (Objects.equals(data.get("version"), version)) throw new RE("当前为最新版本");
        AppInfoDto appInfoDto = new AppInfoDto();
        appInfoDto.downloadUrl = url;
        appInfoDto.version = version;
        appInfoDto.content = content;
        return new R<AppInfoDto>("有新的版本 "+version).setData(appInfoDto);
    }

}
